package com.xgkj.diyiketang.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.SheTuanDetailActivity;
import com.xgkj.diyiketang.activity.data.SheTuanAdapter;
import com.xgkj.diyiketang.data.ClubInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.ClubListTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/6/23.
 */
public class Fragment_NaXin extends BaseFragment implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener {
    private Context mContext;
    private PullToRefreshListView datiListView;
    private SheTuanAdapter mAdapter;
    private int currentPage =1;
    private int size = 10;
    private View emptyView;
    private NoDataUtil noDataUtil;
    private int clubTaskId = -1;

    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shetuan, null);
        mContext = getActivity();
        initView(view);
        return view;
    }

    private void initView(View view) {
        datiListView = (PullToRefreshListView) view.findViewById(R.id.shetuan_listView);
        datiListView.init(mContext, true);
        mAdapter = new SheTuanAdapter(mContext);
        datiListView.setAdapter(mAdapter, true);
        datiListView.setOnRefreshListener(this);
        datiListView.setOnLoadDataListener(this);
        datiListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (datiListView.getHeaderViewsCount() > 0) {
                    position--;
                }

                if (position < 0 || position >= mAdapter.getCount()) return;
                ClubInfo info = (ClubInfo) mAdapter.getItem(position);
                Intent detailIntent = new Intent(mContext, SheTuanDetailActivity.class);
                String clubid = info.getClubId();
                String name = info.getClubName();
                detailIntent.putExtra("CLUBID",clubid);
                detailIntent.putExtra("NAME",name);
                startActivity(detailIntent);
            }
        });
        datiListView.doRefresh();
        emptyView = view.findViewById(R.id.content_layout);
        noDataUtil = new NoDataUtil(mContext,emptyView);
    }

    private void getClubList(){
        if (currentPage ==0){
            datiListView.setNoNextPage();
            return;
        }

        ClubListTask cTask = new ClubListTask(size,currentPage);
        cTask.addListener(new TaskListener(mContext));
        clubTaskId= getEatShowApp().getTaskManager().addTask(cTask);
    }

    class TaskListener extends ShowToastTaskEventListener {
        public TaskListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == clubTaskId) {
                clubTaskId = -1;
                if (currentPage == 1) {
                    mAdapter.clearData();
                    datiListView.onRefreshComplete();
                    datiListView.enableLoadMore();
                } else {
                    datiListView.onLoadDataComplete(true);
                }
                List<ClubInfo> fansList = new ArrayList<ClubInfo>();
                if (data != null) {
                    fansList = (List<ClubInfo>) data;
                }

                if (fansList != null && fansList.size() > 0) {
                    mAdapter.updateData(fansList);
                }

                currentPage = result;
                if (mAdapter != null && mAdapter.getCount() > 0){
                    noDataUtil.ListViewNoEmpty(datiListView);
                }else {
                    noDataUtil.ListViewEmpty(datiListView,getString(R.string.create_your_class),getString(R.string.go_another_class),R.drawable.kongbai);
                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == clubTaskId) {
                clubTaskId = -1;
                if (currentPage == 1) {
                    datiListView.onRefreshComplete();
                } else {
                    datiListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (mAdapter != null && mAdapter.getCount() == 0) {
                datiListView.doRefresh();
            }

        }
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getClubList();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getClubList();
    }
}
